package com.dean.service;

import com.dean.batch.entity.DeanTree;
import com.dean.dao.DeanToolDao;
import com.dean.entity.DataQueryEntity;
import com.dean.utils.DeanJdbcUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DeanJobService {
    private DeanJobService(){
    }
    private static volatile DeanJobService INSTANCE;
    public static DeanJobService getInstance(){
        if(INSTANCE == null){
            synchronized (CodeGenerateService.class){
                if(INSTANCE == null){
                    INSTANCE = new DeanJobService();
                }
            }
        }
        return INSTANCE;
    }

    public DataQueryEntity query(){
        String sql = "select * from sys_batch_job";
        DataQueryEntity dataQueryEntity = DeanJdbcUtils.queryDataAndHead(sql);
        return dataQueryEntity;
    }

    public DeanTree queryChild(DeanTree root){
        List<DeanTree> childs = new ArrayList<DeanTree>();
        String sql = "select * from sys_batch_job_depend where job_id = ? and depend_type = ?";
        List<Map<String,Object>> list = DeanJdbcUtils.queryForList(sql,root.getName(),"DOWN");
        list.forEach(map -> {
            DeanTree node = new DeanTree();
            node.setName(map.get("depend_job_id").toString());
            childs.add(node);
            queryChild(node);
        });
        root.setChild(childs);
        return root;
    }

    public static void main(String[] args) {
        DeanTree  deanTree = new DeanTree();
        deanTree.setName("BATCH_JOB_001");
        DeanJobService.getInstance().queryChild(deanTree);
        System.out.println(deanTree);
    }
}
